home *** CD-ROM | disk | FTP | other *** search
Wrap
property pSprite, pDestination, pPeriod, pOrigTime, pDestTime, pOrigin, pVector, pMoving, pStageBounds, pMaxSpeed, pAvoid, pDistance, pSpeed, pActive, pLimited on getBehaviorDescription me return "AVOID SPRITE" & RETURN & RETURN & "Moves one sprite to maintain a set distance from another. " & "Set the channel of the sprite to avoid, the speed the sprite moves to avoid the other sprite, whether the sprite starts avoid automatically (or waits to be told to start avoiding), and whether its movement is limited to the stage area." & RETURN & RETURN & "PARAMETERS:" & RETURN & "* Channel of sprite to avoid" & RETURN & "* Speed in pixels/second" & RETURN & "* Active (avoiding sprite) at start?" & RETURN & "* Limited to stage area?" end on getBehaviorTooltip me return "Makes a sprite automatically move away from another sprite. " & "You can select which sprite to avoid, the distance to maintain, and the speed at which the sprite will move." end on beginSprite me mInitialize(me) end on prepareFrame me mUpdate(me) mTrackSprite(me) end on mCenter vRect vHalfWidth = vRect.width / 2 vHalfHeight = vRect.height / 2 return point(vRect.left + vHalfWidth, vRect.top + vHalfHeight) end on mCenterOffset vRect vCenter = mCenter(vRect) return vCenter - sprite(pSprite).loc end on mCenteredRect vPoint, vRect vWidth = vRect.width vHeight = vRect.height vHalfWidth = vWidth / 2 vHalfHeight = vHeight / 2 vLeft = vPoint.locH - vHalfWidth vTop = vPoint.locV - vHalfHeight return rect(vLeft, vTop, vLeft + vWidth, vTop + vHeight) end on mFixedLengthVector vVector, vLength vNewVector = vVector * vLength / max(1, mVectorLength(vVector)) return vNewVector end on mInitialize me pSprite = me.spriteNum pMaxSpeed = 1000 pSpeed = min(pMaxSpeed, max(1, integer(pSpeed))) pOrigin = sprite(pSprite).loc + mCenterOffset(sprite(pSprite).rect) pDestination = pOrigin pOrigTime = the milliSeconds pDestTime = pOrigTime + 1 pPeriod = 1 pVector = point(0, 0) pMoving = 0 pStageBounds = point((the stage).rect.width, (the stage).rect.height) mSetDest(pDestination) end on mSetDest vDest if pActive then pDestination = vDest pOrigin = sprite(pSprite).loc + mCenterOffset(sprite(pSprite).rect) pVector = pDestination - pOrigin vDistance = mVectorLength(pVector) pPeriod = max(1, 1000 * vDistance / pSpeed) pOrigTime = the milliSeconds pDestTime = pOrigTime + pPeriod pMoving = 1 end if end on mStageLimit vRect if vRect.left < 0 then vOffsetH = -vRect.left else vOffsetH = min(0, pStageBounds.locH - vRect.right) end if if vRect.top < 0 then vOffsetV = -vRect.top else vOffsetV = min(0, pStageBounds.locV - vRect.bottom) end if return offset(vRect, vOffsetH, vOffsetV) end on mTrackSprite me if pActive then vSprite = the mouseLoc vVector = mVectorAway() if mVectorLength(vVector) < pDistance then if vVector = point(0, 0) then vVector = point((random(2) * 2) - 3, (random(2) * 2) - 3) end if vDest = mCenter(sprite(pAvoid).rect) + mFixedLengthVector(vVector, pDistance) mSetDest(vDest) else pMoving = 0 end if end if end on mUpdate me if pActive then if pMoving then if the milliSeconds > pDestTime then vRect = mCenteredRect(pDestination, sprite(pSprite).rect) if pLimited then vRect = mStageLimit(vRect) end if sprite(pSprite).rect = vRect pMoving = 0 else vElapsed = the milliSeconds - pOrigTime if vElapsed > (pPeriod / 2) then nothing() end if vMoveVector = pVector * vElapsed / pPeriod vRect = mCenteredRect(pOrigin + vMoveVector, sprite(pSprite).rect) if pLimited then vRect = mStageLimit(vRect) end if sprite(pSprite).rect = vRect end if end if end if end on mVectorAway return mCenter(sprite(pSprite).rect) - mCenter(sprite(pAvoid).rect) end on mVectorLength vVector vSquare = (vVector.locH * vVector.locH) + (vVector.locV * vVector.locV) return sqrt(vSquare) end on mSetActive me, vBool if not (not vBool) = vBool then pActive = vBool pMoving = pActive end if end on mSetAvoid me, vAvoid if sprite(vAvoid).type > 0 then pAvoid = vAvoid end if end on mSetDistance me, vDist vDist = max(1, min(600, integer(vDist))) pDistance = vDist end on mSetLimited me, vBool if not (not vBool) = vBool then pLimited = vBool end if end on mSetSpeed me, vSpeed vSpeed = max(1, min(pMaxSpeed, integer(vSpeed))) pSpeed = vSpeed mSetDest(me, pDestination) end on mToggleActive me pActive = not pActive pMoving = pActive end on mToggleLimited me pLimited = not pLimited end on isOKToAttach me, aSpriteType, aSpriteNum return aSpriteType = #graphic end on getPropertyDescriptionList vSpriteRect = sprite(the currentSpriteNum).rect vRadius = max(vSpriteRect.width, vSpriteRect.height) vPDList = [:] setaProp(vPDList, #pAvoid, [#comment: "Avoid Channel", #format: #integer, #default: the currentSpriteNum + 1]) setaProp(vPDList, #pDistance, [#comment: "Distance (10-600 pixels)", #format: #integer, #default: vRadius, #range: [#min: 10, #max: 600]]) setaProp(vPDList, #pSpeed, [#comment: "Speed (20-1000 pixels/sec)", #format: #integer, #default: 1000, #range: [#min: 20, #max: 1000]]) setaProp(vPDList, #pActive, [#comment: "Active at start", #format: #boolean, #default: 1]) setaProp(vPDList, #pLimited, [#comment: "Limited to stage area", #format: #boolean, #default: 1]) return vPDList end